<?php

/**
 * This class manages all HTTP requests concerning user.
 */
class UserController extends Zend_Controller_Action {

    public function init()
    {
       $this->_helper->acl->allow('admin')
	    ->allow('user', array('index', 'list', 'changePw')); 
    }
    
    /**
     * Show all user.
     */
    public function indexAction() 
    {
	$this->listAction();
    }

    /**
     * Show all user.
     */
    public function listAction() 
    {
	$m = new Model_User;
	$this->view->users = $m->listAll();
	$this->view->userCount = count($this->view->users);
	$this->view->usersInactive = $m->listInactive();
	$this->view->userCountInactive = count($this->view->usersInactive);
    }

    /**
     * Create a new user.
     * 
     * Without POST data, an empty form is displayed.
     * With valid POST data, the user is persisted and one gets redirected
     * to the listAction.
     * With invalid POST data, the form's values with error messages is displayed.
     */
    public function createAction() 
    {
	$request = $this->getRequest();
	$form = new Application_Form_User();

	if ($request->isPost()) {
	    if ($form->isValid($request->getPost())) {
		$u = $form->getValues();
		$m = new Model_User;
		$m->create($u);

		return $this->_helper->redirector('list');
	    }
	}
	
	$this->view->form = $form;
    }

    /**
     * Update data of an existing user.
     * 
     * URL parameter id<int> is required.
     * 
     * Without POST data, a form is with the user's data is displayed.
     * With valid POST data, the user is persisted and the user is redirected
     * to /user/list.
     * With invalid POST data, the form's values with error messages is displayed.
     */
    public function updateAction() 
    {
	$request = $this->getRequest();
	$id = (int)$request->getParam('id');
	$form = new Application_Form_UserUpdate();
	$form->validateEmail($id);

	$m = new Model_User;

	if ($request->isPost()) 
	{
	    if ($form->isValid($request->getPost())) 
	    {
		$c = $form->getValues();
		$c['id'] = $id;

		$m->update($c);
		   
		return $this->_helper->redirector('list');
	    }
	}
	else
	{
	    $c = $m->find($id);
	    $form->populate($c);
	}

	$this->view->form = $form;
    }
    
    /**
     * Remove an existing user.
     * 
     * URL parameter id<int> is required. The user is redirected to /user/list
     */
    public function deleteAction() 
    {
	$request = $this->getRequest();
	$id = (int)$request->getParam('id');
	
	$m = new Model_User;
	$m->delete($id);
	
	return $this->_helper->redirector('list');
    }
    
    /**
     * Recovery user password
     */
    public function recpassAction()
    {
	$request = $this->getRequest();
	$id = (int)$request->getParam('id');
	
	$m = new Model_User;
	$m->recpass($id);
	
	return $this->_helper->redirector('list');
    }
    
    public function changepwAction(){
	
	$request = $this->getRequest();
	$form = new Application_Form_UserUpdatePassword();
	
	if ($request->isPost()) {
	    if ($form->isValid($request->getPost())) {
		$p = $form->getValues();
		$m = new Model_User();
		$m->changePw($p);

		return $this->_helper->redirector('list');
	    }
	}
	
	$this->view->form = $form;
    }
    
    public function setaktivAction()
    {
	$request = $this->getRequest();
	$id = (int) $request->getParam('id');
	
	$m = new Model_User();
	$m->setaktiv($id);
	
	return $this->_helper->redirector('list');
    }
    
    public function setinaktivAction()
    {
	$request = $this->getRequest();
	$id = (int) $request->getParam('id');
	
	$m = new Model_User();
	$m->setinaktiv($id);
	
	return $this->_helper->redirector('list');
    }
}